package com.songcha.sakura.ui.adapter

import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.ImageButton
import android.widget.TextView

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.songcha.sakura.R
import com.songcha.sakura.ui.adapter.diff.DownloadTaskDiffCallback
import com.songcha.sakura.download.base.DownloadTask
import com.songcha.sakura.download.m3u8.M3u8DownloadOption
import com.songcha.sakura.download.m3u8.M3u8DownloadTask
import com.songcha.sakura.download.base.TaskState
import com.songcha.library_common.ui.view.loading.CustomProgressBar
import com.songcha.library_common.ui.view.loading.FlowerLoadingView

class DownloadTaskAdapter(val mContext: Context, resId:Int, list:MutableList<DownloadTask>) : BaseQuickAdapter<DownloadTask,BaseViewHolder>(resId,list) {

    init {
         addChildClickViewIds(R.id.ib_control)
    }

    override fun convert(holder: BaseViewHolder, item: DownloadTask, payloads: List<Any>) {
        if(payloads.isEmpty()){
            super.convert(holder, item, payloads)
            return
        }

        item as M3u8DownloadTask
        val payload=payloads[0] as Bundle
        //LogUtil.log("aaa download task convert",item.option.fileName,payload,item.state)

            for(key in payload.keySet()){
                item.option as M3u8DownloadOption
                when(key){
                    DownloadTaskDiffCallback.PAYLOAD_FILE_NAME->{
                        val tvTitle= holder.getView<TextView>(R.id.tv_download_task)
                        if(item.option.fileName.length>10){
                            val title=item.option.fileName.substring(10)
                            tvTitle.text=title
                        }else{
                            tvTitle.text=item.option.fileName
                        }
                    }
                    DownloadTaskDiffCallback.PAYLOAD_RESOLUTION->{
                        if(item.option.resolution.isNotEmpty())
                            holder.getView<TextView>(R.id.tv_resolution).text="("+item.option.resolution+")"
                    }
                   /* DownloadTaskDiffCallback.PAYLOAD_CURRENT_TS->{
                        holder.getView<TextView>(R.id.tv_download_fileSize).text=item.currentTsCount .toString()+"/"+item.totalTsCount.toString()
                    }*/
                    DownloadTaskDiffCallback.PAYLOAD_TOTAL_TS->{
                        holder.getView<TextView>(R.id.tv_download_fileSize).text=item.currentTsCount .toString()+"/"+item.totalTsCount.toString()
                    }
                    DownloadTaskDiffCallback.PAYLOAD_PEOGRESS->{
                        val cpb=holder.getView<CustomProgressBar>(R.id.cpb)
                        cpb.setProgress(item.progress)
                    }
                    //PAYLOAD_CURRENT_TS更改可能会影响PAYLOAD_STATE，所以要一起更新
                    DownloadTaskDiffCallback.PAYLOAD_CURRENT_TS,DownloadTaskDiffCallback.PAYLOAD_STATE->{
                        holder.getView<TextView>(R.id.tv_download_fileSize).text=item.currentTsCount .toString()+"/"+item.totalTsCount.toString()
                        val ibControl=holder.getView<ImageButton>(R.id.ib_control)
                        val flv=holder.getView<FlowerLoadingView>(R.id.flv)
                        if(item.state==TaskState.INITIALIZE){
                            ibControl.visibility=View.INVISIBLE
                            flv.visibility=View.VISIBLE
                        }else if(item.state==TaskState.PRE || item.state==TaskState.COMPLETED){
                            ibControl.visibility=View.INVISIBLE
                            flv.visibility=View.INVISIBLE
                        } else{
                            ibControl.visibility=View.VISIBLE
                            flv.visibility=View.INVISIBLE
                            if(item.state==TaskState.DOWNLOADING){
                                ibControl.setImageResource(R.mipmap.icon_pause)
                            }else if(item.state==TaskState.PAUSE  || item.state==TaskState.ERROR){
                                ibControl.setImageResource(R.mipmap.icon_play)
                            }
                        }
                    }
                }


        }


    }

    override fun convert(holder: BaseViewHolder, item: DownloadTask) {
        item as M3u8DownloadTask
        item.option as M3u8DownloadOption
        val payLoad=Bundle()
        payLoad.putString(DownloadTaskDiffCallback.PAYLOAD_FILE_NAME,item.option.fileName)
        payLoad.putInt(DownloadTaskDiffCallback.PAYLOAD_CURRENT_TS,item.currentTsCount)
        payLoad.putInt(DownloadTaskDiffCallback.PAYLOAD_TOTAL_TS,item.totalTsCount)
        payLoad.putInt(DownloadTaskDiffCallback.PAYLOAD_PEOGRESS,item.progress)
        payLoad.putInt(DownloadTaskDiffCallback.PAYLOAD_STATE,item.state.ordinal)
        payLoad.putString(DownloadTaskDiffCallback.PAYLOAD_RESOLUTION,item.option.resolution)
        convert(holder,item, arrayListOf(payLoad))
    }


}
